<template>
  <div class="maintenance-container">
    <div class="background-decoration">
      <div class="dynamic-background">
        <div class="building building-1"></div>
        <div class="building building-2"></div>
        <div class="building building-3"></div>
        <div class="building building-4"></div>
        <div class="building building-5"></div>
        <div class="cloud cloud-1"></div>
        <div class="cloud cloud-2"></div>
        <div class="cloud cloud-3"></div>
        <div class="sun"></div>
        <div class="light-ray ray-1"></div>
        <div class="light-ray ray-2"></div>
        <div class="light-ray ray-3"></div>
      </div>
      <div class="floating-shape shape-1"></div>
      <div class="floating-shape shape-2"></div>
      <div class="floating-shape shape-3"></div>
      <div class="floating-shape shape-4"></div>
      <div class="floating-shape shape-5"></div>
      <div class="floating-shape shape-6"></div>
    </div>

    <div class="maintenance-wrapper">
      <div class="top-navbar">
        <div class="navbar-left">
          <h1 class="app-title">申请维修</h1>
        </div>
        <div class="navbar-center">
          <el-menu
              :default-active="activeIndex"
              mode="horizontal"
              @select="handleSelect"
              background-color="rgba(255, 255, 255, 0.1)"
              text-color="#fff"
              active-text-color="#4A90E2"
          >
            <el-menu-item index="1" @click="goToHome">首页</el-menu-item>
            <el-menu-item index="2" @click="goToSearch">房源搜索</el-menu-item>
            <el-menu-item index="3" @click="goToMessages">消息中心</el-menu-item>
          </el-menu>
        </div>
        <div class="navbar-right">
          <el-dropdown @command="handleUserCommand">
            <span class="user-info">
              <el-avatar :size="32" src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png" />
              <span class="username">张三</span>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">个人资料</el-dropdown-item>
                <el-dropdown-item command="settings">设置</el-dropdown-item>
                <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>

      <div class="main-content">
        <div class="sidebar">
          <el-menu
              :default-active="activeMenu"
              class="el-menu-vertical"
              background-color="rgba(0, 0, 0, 0.2)"
              text-color="#fff"
              active-text-color="#4A90E2"
          >
            <el-menu-item index="dashboard" @click="goToHome">
              <el-icon><House /></el-icon>
              <span>首页</span>
            </el-menu-item>
            <el-menu-item index="search" @click="goToSearch">
              <el-icon><Search /></el-icon>
              <span>房源搜索</span>
            </el-menu-item>
            <el-sub-menu index="rental">
              <template #title>
                <el-icon><Key /></el-icon>
                <span>租房管理</span>
              </template>
              <el-menu-item index="tenant" @click="goToTenant">我是租客</el-menu-item>
              <el-menu-item index="landlord" @click="goToLandlord">我是房东</el-menu-item>
            </el-sub-menu>
            <el-menu-item index="contract" @click="goToContract">
              <el-icon><Document /></el-icon>
              <span>签约管理</span>
            </el-menu-item>
            <el-menu-item index="profile" @click="goToProfile">
              <el-icon><Setting /></el-icon>
              <span>个人资料</span>
            </el-menu-item>
          </el-menu>
        </div>

        <div class="content-area">
          <div class="maintenance-header">
            <h2>申请维修</h2>
            <p>提交您的房屋维修申请</p>
          </div>

          <div class="maintenance-content">
            <el-card class="maintenance-card">
              <template #header>
                <div class="card-header">
                  <span>维修申请表</span>
                </div>
              </template>

              <div class="maintenance-form">
                <el-form :model="maintenanceForm" label-width="100px">
                  <el-form-item label="房源信息">
                    <el-input v-model="selectedHouseInfo" disabled />
                  </el-form-item>
                  <el-form-item label="故障详情" prop="faultDetail" :rules="[{ required: true, message: '请输入故障详情', trigger: 'blur' }]">
                    <el-input
                        v-model="maintenanceForm.faultDetail"
                        type="textarea"
                        :rows="4"
                        placeholder="请详细描述故障情况"
                    />
                  </el-form-item>

                  <el-form-item label="故障图片">
                    <el-upload
                        class="upload-demo"
                        action="https://jsonplaceholder.typicode.com/posts/"
                        :on-preview="handlePreview"
                        :on-remove="handleRemove"
                        :file-list="fileList"
                        list-type="picture"
                        :limit="3"
                        :on-exceed="handleExceed"
                    >
                      <el-button size="small" type="primary">点击上传</el-button>
                      <template #tip>
                        <div class="el-upload__tip">
                          最多可上传3张图片，每张图片不超过2MB
                        </div>
                      </template>
                    </el-upload>
                  </el-form-item>

                  <el-form-item>
                    <el-button type="primary" @click="submitMaintenance">提交申请</el-button>
                    <el-button @click="resetForm">重置</el-button>
                  </el-form-item>
                </el-form>
              </div>
            </el-card>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  House,
  Search,
  OfficeBuilding,
  User,
  Key,
  ChatLineSquare,
  Setting,
  Document
} from '@element-plus/icons-vue'

const router = useRouter()
const activeMenu = ref('tenant')
const activeIndex = ref('1')

// 租住的房源
const rentedHouses = ref([
  {
    id: 1,
    title: '精致一居室公寓'
  },
  {
    id: 2,
    title: '豪华两居室'
  }
])

// 维修表单
const maintenanceForm = reactive({ 
  house: '', 
  faultDetail: '' 
}) 

const fileList = ref([])

// 上传文件相关方法
const handleRemove = (file, fileList) => { 
  console.log(file, fileList) 
} 

const handlePreview = (file) => { 
  console.log(file) 
} 

const handleExceed = (files, fileList) => { 
  ElMessage.warning('最多只能上传3张图片') 
} 

const submitMaintenance = () => { 
  if (!maintenanceForm.house) { 
    ElMessage.error('请选择房源') 
    return 
  } 

  if (!maintenanceForm.faultDetail.trim()) { 
    ElMessage.error('请输入故障详情') 
    return 
  } 

  ElMessageBox.confirm( 
    '确定要提交维修申请吗？', 
    '申请维修', 
    { 
      confirmButtonText: '确定', 
      cancelButtonText: '取消', 
      type: 'warning' 
    } 
  ).then(() => { 
    // 模拟提交维修申请 
    const formData = new FormData()
    formData.append('house', maintenanceForm.house)
    formData.append('faultDetail', maintenanceForm.faultDetail)
    fileList.value.forEach(file => {
      formData.append('images', file.raw)
    })
    
    // 模拟请求，实际使用时替换为真实接口
    fetch('https://jsonplaceholder.typicode.com/posts/', { 
      method: 'POST', 
      body: formData 
    }) 
    .then(response => response.json()) 
    .then(data => { 
      ElMessage.success('维修申请已提交') 
      
      // 重置表单 
      maintenanceForm.house = '' 
      maintenanceForm.faultDetail = '' 
      fileList.value = [] 
      
      // 返回租客中心 
      router.push('/rental/tenant') 
    }) 
    .catch(error => { 
      ElMessage.error('提交失败，请重试') 
      console.error('Error:', error) 
    }) 
  }).catch(() => { 
    // 用户取消操作 
  }) 
} 

// 顶部导航栏跳转
const goToHome = () => {
  router.push('/')
}

const goToSearch = () => {
  router.push('/house/search')
}

const goToMessages = () => {
  router.push('/message')
}

// 用户命令处理
const handleUserCommand = (command) => {
  switch (command) {
    case 'profile':
      router.push('/profile')
      break
    case 'settings':
      router.push('/settings')
      break
    case 'logout':
      ElMessage.success('退出登录成功')
      localStorage.removeItem('userEmail')
      localStorage.removeItem('isLoggedIn')
      router.push('/login')
      break
  }
}

// 侧边栏跳转
const goToTenant = () => {
  router.push('/rental/tenant')
}

const goToLandlord = () => {
  router.push('/rental/landlord')
}

const goToContract = () => {
  router.push('/contract/tenant')
}

const goToProfile = () => {
  router.push('/profile')
}

// 菜单选择处理
const handleSelect = (key) => {
  switch (key) {
    case '1':
      router.push('/')
      break
    case '2':
      router.push('/house/search')
      break
    case '3':
      router.push('/message')
      break
  }
}

// 重置表单
const resetForm = () => {
  maintenanceForm.house = ''
  maintenanceForm.faultDetail = ''
  fileList.value = []
}

const route = useRoute()
const selectedHouseInfo = ref('')

onMounted(() => {
  selectedHouseInfo.value = `${route.query.houseTitle} - ${route.query.houseAddress}`
  maintenanceForm.house = route.query.rentalId
})
</script>

<style scoped>
.maintenance-container {
  display: flex;
  min-height: 100vh;
  overflow: hidden;
  position: relative;
  background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
}

.background-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  z-index: 0;
}

.dynamic-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(to bottom, #1a2980, #26d0ce);
  overflow: hidden;
}

.building {
  position: absolute;
  bottom: 0;
  background: rgba(30, 30, 50, 0.8);
  border-radius: 5px 5px 0 0;
  box-shadow: 0 -5px 15px rgba(0, 0, 0, 0.3);
}

.building-1 {
  left: 10%;
  width: 80px;
  height: 300px;
  animation: buildingGlow 4s infinite alternate;
}

.building-2 {
  left: 25%;
  width: 120px;
  height: 400px;
  animation: buildingGlow 5s infinite alternate-reverse;
}

.building-3 {
  left: 45%;
  width: 100px;
  height: 350px;
  animation: buildingGlow 6s infinite alternate;
}

.building-4 {
  left: 65%;
  width: 140px;
  height: 450px;
  animation: buildingGlow 4.5s infinite alternate-reverse;
}

.building-5 {
  left: 80%;
  width: 90px;
  height: 320px;
  animation: buildingGlow 5.5s infinite alternate;
}

@keyframes buildingGlow {
  0% {
    box-shadow: 0 -5px 15px rgba(0, 0, 0, 0.3);
  }
  100% {
    box-shadow: 0 -5px 25px rgba(100, 200, 255, 0.6);
  }
}

.cloud {
  position: absolute;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 50%;
  filter: blur(2px);
}

.cloud-1 {
  top: 20%;
  left: 5%;
  width: 80px;
  height: 30px;
  animation: cloudMove 25s linear infinite;
}

.cloud-2 {
  top: 35%;
  left: 70%;
  width: 120px;
  height: 40px;
  animation: cloudMove 30s linear infinite;
}

.cloud-3 {
  top: 15%;
  left: 40%;
  width: 100px;
  height: 35px;
  animation: cloudMove 35s linear infinite;
}

@keyframes cloudMove {
  0% {
    transform: translateX(-100px);
  }
  100% {
    transform: translateX(calc(100vw + 100px));
  }
}

.sun {
  position: absolute;
  top: 80px;
  right: 100px;
  width: 80px;
  height: 80px;
  background: radial-gradient(circle, #ffeb3b, #ffc107);
  border-radius: 50%;
  box-shadow: 0 0 40px #ff9800, 0 0 80px #ff5722;
  animation: sunPulse 3s infinite alternate;
}

@keyframes sunPulse {
  0% {
    transform: scale(1);
    box-shadow: 0 0 40px #ff9800, 0 0 80px #ff5722;
  }
  100% {
    transform: scale(1.1);
    box-shadow: 0 0 60px #ff9800, 0 0 100px #ff5722;
  }
}

.light-ray {
  position: absolute;
  top: 120px;
  right: 140px;
  width: 400px;
  height: 200px;
  background: linear-gradient(to right, transparent, rgba(255, 235, 59, 0.2), transparent);
  transform-origin: left center;
  animation: rayRotate 10s infinite linear;
}

.ray-1 {
  transform: rotate(0deg);
  animation-delay: 0s;
}

.ray-2 {
  transform: rotate(60deg);
  animation-delay: 2s;
}

.ray-3 {
  transform: rotate(120deg);
  animation-delay: 4s;
}

@keyframes rayRotate {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.floating-shape {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  animation: float 15s infinite linear;
}

.shape-1 {
  width: 300px;
  height: 300px;
  top: -100px;
  left: -100px;
  animation-duration: 20s;
  background: radial-gradient(circle, rgba(74, 144, 226, 0.2) 0%, transparent 70%);
}

.shape-2 {
  width: 200px;
  height: 200px;
  bottom: -80px;
  right: -80px;
  animation-duration: 25s;
  animation-direction: reverse;
  background: radial-gradient(circle, rgba(255, 107, 107, 0.2) 0%, transparent 70%);
}

.shape-3 {
  width: 150px;
  height: 150px;
  top: 30%;
  right: 20%;
  animation-duration: 15s;
  background: radial-gradient(circle, rgba(80, 227, 194, 0.2) 0%, transparent 70%);
}

.shape-4 {
  width: 100px;
  height: 100px;
  bottom: 20%;
  left: 20%;
  animation-duration: 18s;
  animation-direction: reverse;
  background: radial-gradient(circle, rgba(255, 209, 102, 0.2) 0%, transparent 70%);
}

.shape-5 {
  width: 250px;
  height: 250px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  animation-duration: 22s;
  background: radial-gradient(circle, rgba(155, 89, 182, 0.2) 0%, transparent 70%);
}

.shape-6 {
  width: 180px;
  height: 180px;
  top: 70%;
  left: 10%;
  animation-duration: 28s;
  background: radial-gradient(circle, rgba(231, 76, 60, 0.2) 0%, transparent 70%);
}

@keyframes float {
  0% {
    transform: translate(0, 0) rotate(0deg);
  }
  25% {
    transform: translate(20px, 20px) rotate(90deg);
  }
  50% {
    transform: translate(0, 40px) rotate(180deg);
  }
  75% {
    transform: translate(-20px, 20px) rotate(270deg);
  }
  100% {
    transform: translate(0, 0) rotate(360deg);
  }
}

.maintenance-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
  z-index: 1;
  position: relative;
}

.top-navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 15px 30px;
  background: rgba(0, 0, 0, 0.2);
  backdrop-filter: blur(10px);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.app-title {
  color: white;
  font-size: 24px;
  font-weight: 700;
  margin: 0;
}

.navbar-center {
  flex: 1;
  margin: 0 20px;
}

:deep(.el-menu--horizontal) {
  border: none;
  background: transparent !important;
}

:deep(.el-menu--horizontal .el-menu-item) {
  color: white;
  border: none;
}

:deep(.el-menu--horizontal .el-menu-item:hover) {
  background: rgba(255, 255, 255, 0.2) !important;
}

:deep(.el-menu--horizontal .el-menu-item.is-active) {
  color: #4A90E2 !important;
  border: none;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  color: white;
}

.username {
  font-size: 16px;
  font-weight: 500;
}

.main-content {
  display: flex;
  flex: 1;
  overflow: hidden;
}

.sidebar {
  width: 250px;
  background: rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(10px);
  padding: 20px 0;
  height: 100%;
  box-shadow: 3px 0 10px rgba(0, 0, 0, 0.2);
}

.el-menu-vertical {
  border: none !important;
  height: 100%;
}

.el-menu-item,
.el-sub-menu__title {
  margin: 5px 10px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.el-menu-item:hover,
.el-sub-menu__title:hover {
  background-color: rgba(255, 255, 255, 0.1) !important;
}

.content-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(10px);
  overflow-y: auto;
  padding: 20px;
}

.maintenance-header {
  margin-bottom: 30px;
}

.maintenance-header h2 {
  color: #2c3e50;
  font-size: 28px;
  margin: 0 0 10px;
}

.maintenance-header p {
  color: #7f8c8d;
  margin: 0;
}

.maintenance-card {
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  max-width: 800px;
  margin: 0 auto;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.maintenance-form {
  padding: 20px;
}

.upload-demo {
  margin-top: 10px;
}

:deep(.el-upload__tip) {
  margin-top: 10px;
  color: #7f8c8d;
}

@media (max-width: 992px) {
  .sidebar {
    width: 80px;
  }

  .app-title,
  .el-menu-item span,
  .el-sub-menu__title span {
    display: none;
  }

  .maintenance-header {
    text-align: center;
  }
}
</style>
